package cn.iocoder.yudao.module.infra.api.file;

import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 文件 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FileApiImpl implements FileApi {

    @Resource
    private FileService fileService;

    @Override
    public String createFile(String path, byte[] content) throws Exception {
        return fileService.createFile(path, content);
    }

    @Override
    public String getOssInfo() {
        FileConfigDO ossInfo = fileService.getOssInfo();
        if(null!=ossInfo && null!= ossInfo.getConfig()){
            FileClientConfig config = ossInfo.getConfig();
            Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().create();
            return gson.toJson(config);
        }
        return null;
    }
}
